package gamePieces;




import gameComponents.Board;
import gameComponents.Player;

import java.awt.Point;
import java.util.ArrayList;

/**
 * Class for the chess piece of a Pawn
 * @author Jordan
 *
 */
public class Pawn extends Piece{
	
	private boolean hasMoved; // used in testing for the double jump at the beginning
	
	/**
	 * Default constructor for Pawn object.
	 * X and Y coordinates of Pawn are set in the superclass Piece. 
	 * 
	 * @param x coordinate of Pawn
	 * @param y coordinate of Pawn
	 */
	public Pawn(int x, int y, Player player) {
		super(x,y, player);
		hasMoved = false;
	}
	
	/**
	 * toString method
	 * @return The string representation of a Pawn on the board.
	 * @author Vivian Andreeva
	 * @modified by Selman Selman
	 */
	public String toString() {
		if (getOwnership().getId()==1) {
		return "P";
		}
		return "p";
	}
	
	/**
	 * Checks if move is legal however it needs to be checked in chess game according to the chess game rules
	 * @author Selman Selman and Jordan Beland
	 * @return true if the move is legal
	 */
	public boolean checkIfLegal(int x, int y){
		if(x < 0 || x > Board.SIZE){
			return false;
		} else if(y < 0 || y > Board.SIZE){
			return false;
		} else{
			int xDiff = Math.abs(super.getX() - x);
			int yDiff = Math.abs(super.getY() - y);
			if((yDiff == 1 && xDiff ==0) || (yDiff == 2 && xDiff == 0 && !hasMoved) || (xDiff == 1 && yDiff == 1)){ 
				return true;
			}
		}
		return false;
		}

	@Override
	/**
	  * Overrides abstract method in superclass Piece.
	  * Calculates list of all possible moves of
	  * the Pawn object at its current position.
	  * Pawn piece can move forwared two positions if it is at its starting position,
	  * one move forward otherwise, and diagonally only if it can eat another piece.
	  * @author Vivian Andreeva
	  * @return an ArrayList of all the points the pawn could IDEALLY move to.
	  */
	public ArrayList<Point> possibleMoves() {
		int x = super.getX();
		int y = super.getY();
		moves = new ArrayList<Point>();
		
		if(x < Board.SIZE && y+1 < Board.SIZE){
			moves.add(new Point(x, y+1)); // one move forward
		}
		if(x < Board.SIZE && y+2 < Board.SIZE && !hasMoved()){
			moves.add(new Point(x, y+2)); // two move forward (if pawn is at starting point)
		}
		if(x+1 < Board.SIZE && y +1 < Board.SIZE){
			moves.add(new Point(x+1, y+1)); // positive diagonal (if piece can be eaten)
		}
		if(x-1 >= 0 && y +1 < Board.SIZE){
			moves.add(new Point(x-1, y+1)); // negative diagonal (if piece can be eaten)
		}
		return moves;
	}
	
	/**
	 * Overwrites move method in Piece to also change its
	 * state of being moved, no longer allowing it to complete
	 * a double jump
	 * 
	 * @param x coordinate of new location
	 * @param y coordinate of new location
	 * @author Jordan Beland
	 */
	public void move(int x, int y){
		super.move(x,y);
		hasMoved = true;
	}
	
	/**
	 * returns boolean hasMoved in order to check for
	 * ability to double jump
	 * @return true if it has moved
	 * @author Jordan Beland
	 */
	public boolean hasMoved(){
		return this.hasMoved;
	}
}
